package com.zym.init.exception;

import cn.dev33.satoken.exception.NotLoginException;
import com.zym.init.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.NoResourceFoundException;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {

    /**
     * 未登录异常
     */
    @ExceptionHandler(NotLoginException.class)
    public AjaxResult businessExceptionHandler(NotLoginException e) {
        log.error("NotLoginException", e);
        return AjaxResult.login();
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(RuntimeException.class)
    public AjaxResult runtimeExceptionHandler(RuntimeException e) {
        log.error("RuntimeException", e);
        return AjaxResult.error(e.getMessage());
    }

    /**
     * 角色异常
     */
    @ExceptionHandler(RoleException.class)
    public AjaxResult roleExceptionHandler(RoleException e) {
        log.error("RoleException", e);
        return AjaxResult.permission();
    }

    /**
     * 资源不存在异常
     */
    @ExceptionHandler(NoResourceFoundException.class)
    public AjaxResult noResourceFoundExceptionHandler(NoResourceFoundException e) {
        log.error("NoResourceFoundException", e);
        return AjaxResult.notFound();
    }

    /**
     * 账号被封禁异常
     */
    @ExceptionHandler(BannedException.class)
    public AjaxResult bannedExceptionHandler(BannedException e) {
        log.error("BannedException", e);
        return AjaxResult.permission(e.getMessage());
    }
}
